<template>
  <div class='container'>
    <div class="app-container">
      <el-form
          :inline="true"
          v-model="subjectsRuleForm"
        >

          <el-form-item label="学科名称" label-width="80px">
            <el-input
              v-model="subjectsRuleForm.subjectName"
              class="input-width"
            />
          </el-form-item>

          <el-form-item class="btns">
            <el-button @click="clear">清除</el-button>
            <el-button type="primary" @click="search">搜索</el-button>
          </el-form-item>

          <el-form-item class="addSubjects">
            <el-button type="success" @click="onAddSubjects">
              <i class="el-icon-edit"></i>
              新增学科
            </el-button>
          </el-form-item>
        </el-form>
    </div>
    <div class="total">
      <i class="el-icon-info"><span class="total-font">共{{ subjectsList.counts }}条记录</span></i>
    </div>
    <my-table
      :table-props="{
          data: subjectsList.items,
          headerCellStyle: getRowClass
        }"
        :columns="[
          { label: '序号', prop: 'id'},
          { label: '学科名称', prop: 'subjectName'},
          { label: '创建者', prop: 'username'},
          { label: '创建日期', prop: 'addDate'},
          { label: '前台是否显示', prop: 'isFrontDisplay'},
          { label: '二级目录', prop: 'totals'},
          { label: '标签', prop: 'tags'},
          { label: '题目数量', prop: 'twoLevelDirectory'},
          { label: '操作', slotName: 'operators', width: 300}
        ]"
          :pagination-props="{
          pageSize: pageSize,
          pageSizes: [5, 10, 15, 20],
          background: true,
          total: subjectsList.counts,
          layout: '->, prev, pager, next, sizes, jumper'
        }"
        :pagination-events="events"
      >
    >
      <template v-slot:operators="scope">
        <el-button type="text" @click="goDirectorys(scope)">学科分类</el-button>
        <el-button type="text" @click="goTags(scope)">学科标签</el-button>
        <el-button type="text" @click="editSubjects(scope)">修改</el-button>
        <el-button type="text" @click="deleteInfo(scope)">删除</el-button>
      </template>
    </my-table>

    <!-- 新增用户弹窗 -->
    <el-dialog
      title= "修改学科"
      :visible.sync="editSubjectsVisible"
      width="400px"
    >
      <el-form
        :model="editSubjectsRuleForm"
        label-width="80px"
      >
        <el-form-item label="学科名称" required>
          <el-input
            v-model="editSubjectsRuleForm.name"
          />
        </el-form-item>

        <el-form-item label="是否显示" required>
          <el-switch
            v-model="editSubjectsRuleForm.isshow"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
      </el-form>

      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" @click="oneditSubjectsOk">确定</el-button>
          <el-button @click="oneditSubjectsClose">取消</el-button>
        </el-col>
      </el-row>

    </el-dialog>

  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'
import { remove, update } from '@/api/hmmm/subjects'

export default {
  data () {
    return {
      events: {
        'current-change': this.onCurrentPageChange,
        'size-change': this.onPageSizeChange
      },
      subjectsRuleForm: {
        subjectName: ''
      },
      page: 1,
      pageSize: 10,
      editSubjectsVisible: false,
      editSubjectsRuleForm: {
        name: '',
        isshow: false,
        id: -1
      }
    }
  },
  computed: {
    ...mapState('subjects', ['subjectsList'])
  },
  created () {
    this.updateSubjectsList({
      page: this.page,
      pageSize: this.pageSize
    })
  },
  methods: {
    ...mapActions('subjects', [
      'updateSubjectsList'
    ]),
    ...mapActions('subjects', [
      'updateSubjectsType'
    ]),
    clear () {
      this.subjectsRuleForm = {
        subjectName: ''
      }
    },
    // 搜索
    search () {
      let f = 0
      const form = {}
      for (const key in this.subjectsRuleForm) {
        if (this.subjectsRuleForm[key] !== '') {
          f = 1
          form[key] = this.subjectsRuleForm[key]
        }
      }
      if (f) {
        this.updateSubjectsList({
          ...form,
          page: this.page,
          pageSize: this.pageSize
        })
      } else {
        this.updateSubjectsList({
          page: this.page,
          pageSize: this.pageSize
        })
      }
    },
    onAddSubjects () {

    },
    onCurrentPageChange (page) {
      this.page = page
      this.updateSubjectsList({
        page: this.page,
        pageSize: this.pageSize
      })
    },
    onPageSizeChange (pageSize) {
      this.pageSize = pageSize
      this.page = 1
      this.updateSubjectsList({
        page: this.page,
        pageSize: this.pageSize
      })
    },
    getRowClass ({ rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return 'background:#f8f8f9; border-bottom: 2px solid #dcdcdc'
      }
    },
    // 学科分类按钮
    goTags ({ row }) {
      this.$router.push({
        name: 'subjects-tags',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    },
    goDirectorys ({ row }) {
      this.$router.push({
        name: 'subjects-directorys',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    },
    oneditSubjectsOk () {
      this.editSubjectsVisible = false
      console.log(this.editSubjectsRuleForm.isshow)
      // if (this.editSubjectsRuleForm.isshow)
      // update(this.editSubjectsRuleForm.id)
    },
    oneditSubjectsClose () {
      this.editSubjectsRuleForm = {
        name: '',
        isshow: false,
        id: -1
      }
      this.editSubjectsVisible = false
    },
    editSubjects ({ row }) {
      const isFrontDisplay = row.isFrontDisplay === '是' ? true : false
      this.editSubjectsRuleForm.isshow = isFrontDisplay
      this.editSubjectsRuleForm.id = row.id
      this.editSubjectsVisible = true
    },
    // 删除
    deleteInfo ({ row }) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove(row)
          .then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        this.updateSubjectsList({
          page: this.page,
          pageSize: this.pageSize
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }

  }
}
</script>

<style scoped lang='sass'>
.container
  background-color: #fff
  padding: 20px
  .app-container
    padding: 0
.addSubjects
  float: right
.total
    font-size: 14px
    background-color: #eee
    padding: 8px
    width: calc(100% - 8px)
    border-radius: 5px
    margin-bottom: 20px
    color: #666
    .total-font
      margin-left: 6px
.el-col-6
  width: 100%

</style>
